package com.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bean.Proveedor;
import com.dao.RegistroDao;

@Repository("ProveedorDaoImpl")
public class ProveedorDaoImpl implements RegistroDao{
	Logger log	= Logger.getLogger(ProveedorDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	@Transactional(readOnly = true)
	public boolean agregaRegistro(Object registro) {
		log.debug("begin");
		Proveedor proveedor;
		Session session	= SessionFactoryUtils.getSession(sessionFactory, true);
		Transaction tr	= session.beginTransaction();
		proveedor	= (Proveedor) registro;	
		session.save(proveedor);
		tr.commit();
		log.debug("finally");
		return false;
	}

	@Override
	@Transactional(readOnly = true)
	public boolean eliminaRegistro(Object registro) {
		Session session	= SessionFactoryUtils.getSession(sessionFactory, true);
		Transaction tr	= session.beginTransaction();
		
		session.delete(registro); 
		tr.commit();
		return false;
	}

	@Override
	@Transactional(readOnly = true)
	public void modificaRegistro(Object registro) {
		// TODO Auto-generated method stub
		
	}


	@Override
	@Transactional(readOnly = true)
	public Object buscaRegistroPorParametro(String param, Object registro) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public <T> List<T> listaDeRegistros() {
		log.debug("begin");
		Session session	= SessionFactoryUtils.getSession(sessionFactory, true);
		Criteria criterioBusqueda	= session.createCriteria(Proveedor.class);
		log.debug("finally");
		return criterioBusqueda.list();
	}

}
